package com.atguigu.controller;


import com.atguigu.entity.CartInfo;
import com.atguigu.result.RetVal;
import com.atguigu.service.CartInfoService;
import com.atguigu.util.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 购物车表 用户登录系统时更新冗余 前端控制器
 * </p>
 *
 * @author GuoJH
 * @since 2021-09-04
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartInfoService cartInfoService;
    //1.将商品添加到购物车
    @PostMapping("addToCart/{skuId}/{skuNum}")
    public RetVal addToCart(@RequestParam Long skuId,
                            @RequestParam Integer skuNum,
                            HttpServletRequest request) {
        //拿到用户的id
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            //userId为空  则未登录 在获取临时id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //购物车的添加
        cartInfoService.addToCart(skuId,userId,skuNum);
        return RetVal.ok();
    }
    //2.购物车的列表
    @GetMapping("/getCartList")
    public RetVal getCartList(HttpServletRequest request) {
        //拿到用户的id
        String userId = AuthContextHolder.getUserId(request);//登录的
        String userTempId = AuthContextHolder.getUserTempId(request);//未登录 临时id
        List<CartInfo> cartInfoList = cartInfoService.getCartList(userId,userTempId);
        return RetVal.ok(cartInfoList);
    }
    //3.更新商品的选中状态
    @GetMapping("/checkCart/{skuId}/{isSelected}")
    public RetVal checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isSelected,
                            HttpServletRequest request){
        //选中状态 也分为 未登录 和登录
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.checkCart(userId,skuId,isSelected);
        return RetVal.ok();
    }
    //4.购物车中商品的删除
    @DeleteMapping("/deleteCart/{skuId}")
    public RetVal deleteCart(@PathVariable Long skuId,HttpServletRequest request){
        //商品删除 分 登录前 和登录后
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.deleteCart(userId,skuId);
        return RetVal.ok();
    }
    //5.查询购物车的选中信息
    @GetMapping("/getSelectedProduct/{userId}")
    public List<CartInfo> getSelectedProduct(@PathVariable String userId){
        return cartInfoService.getSelectedProduct(userId);
    }
    //6.从数据库中查询缓存信息
    @GetMapping("/queryCartInfoFromDbToRedis/{userId}")
    public List<CartInfo> queryCartInfoFromDbToRedis(@PathVariable String userId){
        return cartInfoService.queryCartInfoFromDbToRedis(userId);
    }


}

